﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using TestVisioCanvas.Wrappers;
using System.Xml;
using System.Xml.XPath;

namespace TestVisioCanvas.ApplicationState
{
    public class AppStateManager
    {
        // maintains the informations about all the all the pages of the drawing control
        private List<PageState> AppState;
        private int _pagescount;
        private string _customerName;
        public string ExcelTemplateFileName { get; private set; }
        public int ExcelTemplateStartingRow {get;private set;}
        
        public AppStateManager()
        {
            AppState = new List<PageState>();
            _pagescount = AppState.Count;

            XPathDocument doc = new XPathDocument(Environment.CurrentDirectory+"//Configuration//Conf.xml");
            XPathNavigator nav = doc.CreateNavigator();
            XPathNodeIterator result = nav.Select("/Configuration/TemplateExcel/*");
            result.MoveNext();
            ExcelTemplateFileName = result.Current.Value;
            result.MoveNext();
            ExcelTemplateStartingRow = int.Parse(result.Current.Value);                        
        }

        public string CustomerName
        {
            get { return _customerName; }
            set { _customerName = value; }
        }

        public PageState this[int PageIndex]
        {
            get 
            {
                return AppState.FirstOrDefault(x => x.PageID == PageIndex);                
            }
        }

        public IEnumerable<PageState> PagesAsEnumerable
        {
            get { return (AppState.AsEnumerable()); }
        }

        public void PageAdded(int PageID, string PageName)
        {
            if (AppState.FirstOrDefault(x=>x.PageID==PageID) != null)
            {
#warning sostituire con un'eccezione più indicativa
                throw new Exception("Duplicate page ID");
            }
            else
            {
                AppState.Add(new PageState(PageID, PageName));
            }
        }

        public void PageRemoved(int PageID)
        {
            var item = AppState.FirstOrDefault(x => x.PageID == PageID);
            if (item!=null)
            {
                AppState.Remove(item);
            }
            else
            {
#warning sostituire con un'eccezione più indicativa
                throw new Exception("Page ID not present");
            }
        }

        public int PagesCount
        {
            get { return this.AppState.Count; }
        }


            
    }
}
